

import java.util.ArrayList;

public class WeatherData implements Subject{
    private double temperature;
    private double pressure;
    private double humidity;
    private ArrayList<Observer> observers = new ArrayList<>();

    public double getTemperrature() {
        return temperature;
    }

    public double getPressure() {
        return pressure;
    }

    public double getHumidity() {
        return humidity;
    }

    public void dataChange(){
        notifyObservers();
    }

    public void setWeatherData(double temperature,double pressure,double humidity){
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        dataChange();       //通知观察者数据改变
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        if (observers.contains(o)){
            observers.remove(o);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(this.temperature,this.pressure,this.humidity);
        }
    }
}
